<template>
  <div class="app-container">
    <div class="filter-container">
      <SearchOptionUser :type="2" @setSearchUserId="searchUserId" />
      <SearchOptionClass :class-id="listQuery.class_id" @setSearchClassId="searchClassId" />
      <el-select v-model="listQuery.type" placeholder="状态" clearable class="filter-item" style="width: 130px">
        <el-option v-for="(value,key) in typeOptions" :key="key" :label="value" :value="key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves type="primary" style="float:right" @click="handleAddStudent">
        调入学员
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="选修课表ID" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.xuanxiu_timetable_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.courseInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.userInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班级名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.classInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生类型" align="center">
        <template slot-scope="{row}">
          <span>{{ typeOptions[row.type] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button :type="row.type==0||row.type==1?'success':row.type==2?'primary':'danger'" size="mini" @click="handleUserOut(row,$index)">
            {{ row.type==1?'调出':(stuTypeOptions[row.type]) }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog title="调入学员" :visible.sync="showStu">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:50px;">
        <el-form-item label="学员" prop="user_id">
          <el-cascader
            v-model="temp.user_id"
            :options="classUserListOptions"
            :props="props"
            clearable
            filterable
          />
        </el-form-item>
        <el-form-item label="班级" prop="class_id">
          <el-select v-model="temp.class_id" class="filter-item">
            <el-option v-for="item in courseTimetableClassInfo" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showStu = false">
          取消
        </el-button>
        <el-button type="primary" @click="addStudentData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { timetableUserPage, timetableUsreOut, timetableUsreIn } from '@/api/xuanxiu'
import { notifySuccess, my_confirm, listQueryFilter, getFileUrl } from '@/utils/tool'
import { schoolUserList } from '@/api/course'
import waves from '@/directive/waves' // waves directive
import SearchOptionClass from '@/components/SearchOption/class'
import SearchOptionUser from '@/components/SearchOption/user'
import Pagination from '@/components/Pagination'

export default {
  name: 'XuanxiuTimetableUser',
  components: { Pagination, SearchOptionClass, SearchOptionUser },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        xuanxiu_timetable_id: this.$route.query.xuanxiu_timetable_id,
        user_id: null,
        class_id: null,
        type: '',
        querySort: '-id'
      },
      typeOptions: {
        1: '本班',
        2: '调出',
        3: '调入'
      },
      stuTypeOptions: { 0: '调出', 2: '调回本班', 3: '移除' },
      temp: {
        course_timetable_id: this.$route.query.id,
        user_id: '',
        class_id: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
      },
      showStu: false,
      courseTimetableClassInfo: [],
      classUserListOptions: [],
      downloadLoading: false,
      props: { children: 'child', label: 'name', value: 'user_id', emitPath: false }
    }
  },
  created() {
    this.getUserInDialog()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      timetableUserPage(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.page
        this.courseTimetableClassInfo = response.data.courseTimetableClassInfo
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUserOut(row, index) {
      const params = {
        user_id: row.user_id,
        xuanxiu_timetable_id: row.xuanxiu_timetable_id
      }
      my_confirm(this, () => {
        timetableUsreOut(params).then(() => {
          this.list.splice(index, 1)
          this.dialogFormVisible = false
          this.getList()
          notifySuccess(this, '移除成功')
        })
      })
    },
    resetTemp() {
      this.temp = {
        xuanxiu_timetable_id: this.$route.query.xuanxiu_timetable_id,
        user_id: '',
        class_id: ''
      }
    },
    handleAddStudent() {
      this.resetTemp()
      this.showStu = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    addStudentData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          timetableUsreIn(this.temp).then(() => {
            notifySuccess(this, '创建成功')
            this.getList()
          })
        }
      })
    },
    getUserInDialog() {
      schoolUserList().then(response => {
        this.classUserListOptions = response.data
      })
    },
    returnImageUrl(photo) {
      return getFileUrl(photo)
    },
    searchClassId: function(data) {
      this.listQuery.class_id = data.classId
    },
    searchUserId: function(data) {
      this.listQuery.user_id = data.userId
    }
  }
}
</script>
